package com.llb.utils;


import com.llb.annotation.ImportColumn;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ParserUtil
 * @Description TODO
 * @Author liang
 * @Date 2019/3/22 22:24
 * @Version 1.0
 **/
public class ParserUtil {


    /**
     * 将一行数据转成一个对象
     * @param row
     * @param clazz
     * @return
     */
    public static Object rowConverToEntity(Row row, Class clazz) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        //存放单元格内容
        List<String> cellList = new ArrayList();
        //需要读取多少列又bean的属性决定
        List<Field> fields = AnnotationUtils.getAnnotationFieldsInClass(ImportColumn.class, clazz);
        int size = fields.size();
        for(int i = 0;i < size;i++){
            //第i列单元格内容
            Cell cell = row.getCell(i);
            //格式转换
            String cellValue = getCellValue(cell);
            cellList.add(cellValue);
        }
        Object obj = convert(cellList,clazz);
        return obj;
    }

    /**
     * list内容转成实体对象
     * @param cellList
     * @param clazz
     * @return
     */
    private static Object convert(List<String> cellList, Class clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        List<Field> fields = AnnotationUtils.getAnnotationFieldsInClass(ImportColumn.class, clazz);
        //属性条数
        int size = fields.size();
        //获取所有方法
        Method[] methods = clazz.getMethods();
        Object obj = clazz.newInstance();
        for(int i  = 0; i < size; i ++){
            //单元格内容
            String cellValue = cellList.get(i);
            //属性名,替换成setXxx
            String name = fields.get(i).getName();
            StringBuffer sb = new StringBuffer(name);
            sb.replace(0,1,(name.charAt(0)+"").toUpperCase());
            String methName = "set" + sb.toString();
            //找到相应的method方法,并实例化
            for(Method m : methods){
                if(methName.equals(m.getName())){
                    m.invoke(obj,cellValue);
                    break;
                }

            }
         }
         return obj;
    }


    /***
     * 获取cell内容
     * 根据单元格不同格式,转换成String
     * @param cell
     * @return
     */
    private static String  getCellValue(Cell cell){
        String value = "";
        if(cell != null){
            int cellType = cell.getCellType();
            switch (cellType){
                case Cell.CELL_TYPE_NUMERIC:
                    //日期类型
                    if(HSSFDateUtil.isCellDateFormatted(cell)){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                        value = sdf.format(date);
                    }else {
                        //Double data = cell.getNumericCellValue();
                        //value = data.toString();
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        value = cell.getStringCellValue();
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    Boolean data = cell.getBooleanCellValue();
                    value = data.toString();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    value = String.valueOf(cell.getNumericCellValue());
                    if(value.equals("NaN")){
                        value = cell.getStringCellValue().toString();
                    }
                    break;
                case Cell.CELL_TYPE_BLANK:
                    //System.out.println("单元格内容为空");
                    break;
                case Cell.CELL_TYPE_ERROR:
                    System.out.println("单元格内容出错");
                    break;
                default:
                    value = cell.getStringCellValue().toString();
                    break;
            }

        }
        return value.trim();
    }
}
